package com.company.control;

import com.company.bean.UserInfoBean;
import com.company.model.UserModel;
import com.company.util.Myutils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "UserInfoQueryServlet", value = "/UserInfoQueryServlet")
public class UserInfoQueryServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.获取表单数据

        UserInfoBean userInfoBean = Myutils.getNewIntance(UserInfoBean.class);
        userInfoBean.setUserName(request.getParameter("userName"));
        userInfoBean.setUserPhone(request.getParameter("userPhone"));

        //把从前台页面获取的表单数据的值转换为日期Date类型
        //开始日期----把从前台页面获取的表单数据的值转换为日期Date类型
        String strUserRegisterDate = request.getParameter("userRegisterDate");

        if (StringUtils.isNotEmpty(strUserRegisterDate)){
            userInfoBean.setUserRegisterDate(
                    Myutils.convertString2Date(strUserRegisterDate,"yyyy-MM-dd"));
        }

        //查询条件 ---- 截止日期
        //----把从前台页面获取的表单数据的值转换为日期Date类型
        String strUserRegisterDate2 = request.getParameter("userRegisterDate2");
        if (StringUtils.isNotEmpty(strUserRegisterDate2)){
            userInfoBean.setUserRegisterDate2(
                    Myutils.convertString2Date(strUserRegisterDate2,"yyyy-MM-dd"));
        }

        //2.调用model层进行业务逻辑查询处理
        //调用model层进行查询操作，将用户数据查询出来，保存到session中
        List<UserInfoBean> listUsers = Myutils.getNewIntance(UserModel.class)
                .queryUserInfoByParams(userInfoBean);

        //跳转之前，需要将显示在成功页面的数据进行session缓存
        request.getSession().setAttribute("listUsers", listUsers);


        //3.显示查询结果页面
        request.getRequestDispatcher("WEB-INF/viewpages/userinfoB/UserInfoAddDetail.jsp")
                .forward(request, response);

    }

    }

